from PyQt5.QtWidgets import QBoxLayout, QButtonGroup, QLineEdit, QPushButton, QRadioButton, QWidget,QDesktopWidget,QApplication,QPushButton,QLabel
from PyQt5.QtWidgets import QGridLayout,QFileDialog
import sys
from PyQt5 import QtGui



class Main(QWidget):
    def __init__(self,parent=None) :
        super(Main,self).__init__(parent)
        self.setWindowTitle('主界面')
        self.setFixedSize(500,500)
        self.center()
        #新建控件
        self.input_label=QLabel("输入")
        self.input_lineEdit=QLineEdit()
        self.insert_buttuon=QPushButton('插入')
        self.refresh_button=QPushButton('刷新')
        self.invalid_radio=QRadioButton('无效')
        self.invalid_radio.setChecked(1)
        self.bad_radio=QRadioButton("差")
        self.medium_radio=QRadioButton("中")
        self.good_radio=QRadioButton("优")
        self.radio_group=QButtonGroup()
        self.radio_group.addButton(self.invalid_radio,0)
        self.radio_group.addButton(self.bad_radio,1)
        self.radio_group.addButton(self.medium_radio,2)
        self.radio_group.addButton(self.good_radio,3)
        self.invalid_button=QPushButton('无效数据')
        self.invalid_adress_lineEdit=QLineEdit()
        self.bad_button=QPushButton('差数据')
        self.bad_adress_lineEdit=QLineEdit()
        self.medium_button=QPushButton('中等数据')
        self.medium_adress_lineEdit=QLineEdit()
        self.good_button=QPushButton('好数据')
        self.good_adress_lineEdit=QLineEdit()
        self.count_label=QLabel('统计')
        self.invalid_label=QLabel("无效数据统计")
        self.invalid_lineEdit=QLineEdit()
        self.bad_label=QLabel("差数据统计")
        self.bad_lineEdit=QLineEdit()
        self.medium_label=QLabel("中等数据统计")
        self.medium_lineEdit=QLineEdit()
        self.good_label=QLabel("好数据统计")
        self.good_lineEdit=QLineEdit()
        
        grid_layout=QGridLayout()

        grid_layout.addWidget(self.input_label,0,0,1,1)
        grid_layout.addWidget(self.input_lineEdit,0,1,1,5)
        grid_layout.addWidget(self.insert_buttuon,1,0,1,1)
        grid_layout.addWidget(self.refresh_button,1,1,1,1)
        grid_layout.addWidget(self.invalid_radio,1,2,1,1)
        grid_layout.addWidget(self.bad_radio,1,3,1,1)
        grid_layout.addWidget(self.medium_radio,1,4,1,1)
        grid_layout.addWidget(self.good_radio,1,5,1,1)
        grid_layout.addWidget(self.invalid_button,2,0,1,1)
        grid_layout.addWidget(self.invalid_adress_lineEdit,2,1,1,5)
        grid_layout.addWidget(self.bad_button,3,0,1,1)
        grid_layout.addWidget(self.bad_adress_lineEdit,3,1,1,5)
        grid_layout.addWidget(self.medium_button,4,0,1,1)
        grid_layout.addWidget(self.medium_adress_lineEdit,4,1,1,5)
        grid_layout.addWidget(self.good_button,5,0,1,1)
        grid_layout.addWidget(self.good_adress_lineEdit,5,1,1,5)
        grid_layout.addWidget(self.count_label,6,0,1,1)
        grid_layout.addWidget(self.invalid_label,7,0,1,1)
        grid_layout.addWidget(self.invalid_lineEdit,7,1,1,5)
        grid_layout.addWidget(self.bad_label,8,0,1,1)
        grid_layout.addWidget(self.bad_lineEdit,8,1,1,5)
        grid_layout.addWidget(self.medium_label,9,0,1,1)
        grid_layout.addWidget(self.medium_lineEdit,9,1,1,5)
        grid_layout.addWidget(self.good_label,10,0,1,1)
        grid_layout.addWidget(self.good_lineEdit,10,1,1,5)
        self.setLayout(grid_layout)
        # 参数设置
        self.invalid_adress_lineEdit.setText('C:/Users/Administrator/Desktop/无效.txt')
        self.bad_adress_lineEdit.setText('C:/Users/Administrator/Desktop/差.txt')
        self.medium_adress_lineEdit.setText('C:/Users/Administrator/Desktop/中.txt')
        self.good_adress_lineEdit.setText('C:/Users/Administrator/Desktop/优.txt')
        # 设置connect连接
        self.invalid_button.clicked.connect(self.openfile_invalid)
        self.bad_button.clicked.connect(self.openfile_bad)
        self.medium_button.clicked.connect(self.openfile_medium)
        self.good_button.clicked.connect(self.openfile_good)
        self.refresh_button.clicked.connect(self.refresh_show)
        self.insert_buttuon.clicked.connect(self.insert_action)
        #connect调用有参数的self都要用lambda
        #self.change_color_button.clicked.connect(lambda:self.set_color(255,255,255))
    def center(self):
        screen = QDesktopWidget().screenGeometry()
        size = self.geometry()
        self.move((screen.width() - size.width()) / 2,
                  (screen.height() - size.height()) / 2)
    def openfile_invalid(self):
        openfile_name,file_type = QFileDialog.getOpenFileName(self,'选择文件','','Text files(*.txt )')
        self.invalid_adress_lineEdit.setText(openfile_name)
    def openfile_bad(self):
        openfile_name,file_type = QFileDialog.getOpenFileName(self,'选择文件','','Text files(*.txt )')
        self.bad_adress_lineEdit.setText(openfile_name)
    def openfile_medium(self):
        openfile_name,file_type = QFileDialog.getOpenFileName(self,'选择文件','','Text files(*.txt )')
        self.medium_adress_lineEdit.setText(openfile_name)
    def openfile_good(self):
        openfile_name,file_type = QFileDialog.getOpenFileName(self,'选择文件','','Text files(*.txt )')
        self.good_adress_lineEdit.setText(openfile_name) 
    def refresh_show(self):
        with open(self.invalid_adress_lineEdit.text(), 'r') as f:
            self.invalid_lineEdit.setText(str(len(f.readlines())))
            f.close
        with open(self.bad_adress_lineEdit.text(), 'r') as f:
            self.bad_lineEdit.setText(str(len(f.readlines()))+"/300")
            f.close
        with open(self.medium_adress_lineEdit.text(), 'r') as f:
            self.medium_lineEdit.setText(str(len(f.readlines()))+"/1200")
            f.close
        with open(self.good_adress_lineEdit.text(), 'r') as f:
            self.good_lineEdit.setText(str(len(f.readlines()))+"/500")
            f.close
    def insert_action(self):
        
        if self.invalid_radio.isChecked()==1:
            with open(self.invalid_adress_lineEdit.text(), 'r+') as f:
                content = f.read()        
                f.seek(0, 0)
                f.write(self.input_lineEdit.text()+'\n'+content)
                f.close
        if self.bad_radio.isChecked()==1:
            with open(self.bad_adress_lineEdit.text(), 'r+') as f:
                content = f.read()        
                f.seek(0, 0)
                f.write(self.input_lineEdit.text()+'\n'+content)
                f.close
        if self.medium_radio.isChecked()==1:
            with open(self.medium_adress_lineEdit.text(), 'r+') as f:
                content = f.read()        
                f.seek(0, 0)
                f.write(self.input_lineEdit.text()+'\n'+content)
                f.close
        if self.good_radio.isChecked()==1:
            with open(self.good_adress_lineEdit.text(), 'r+') as f:
                content = f.read()        
                f.seek(0, 0)
                f.write(self.input_lineEdit.text()+'\n'+content)
                f.close
        self.refresh_show()


        
    

        
if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = Main()
    main.show()
    sys.exit(app.exec_())
